js.underas.controller.Select = new Class({
	"Extends":js.underas.controller.InputDouble
	,"_valuefield":"id"
	,"_labelfield":"label"
	,"initialize":function(p_placeholder){		
		var p_placeholder = p_placeholder||"";
		this.parent("text","");
		this.getEle().addClass("Select");
		this.setIcon("search",1);
		this.setIcon("triangle-bottom",2);
		this.setPlaceHolder(p_placeholder);
		this.getEle().appendHTML('<ul id="uid_'+this.uid+'_2" style="z-index:9999;top:56px;left:18px;display:none;max-height:160px;overflow:auto" class="select-content dropdown-menu" role="menu"></ul>');
		this.addEvent('keyup',this.setFilter.bind(this));
		
		/*
		this.getInput().addEvent('blur',function(evt){
					evt.preventDefault();
					this.showList(false);
		}.bind(this));
		*/
		//this.getEle().addEvent('click',function(evt){evt.preventDefault();this.showList(true);}.bind(this));	
		this.getEle(".addon2").addEvent('click',this.reSizeList.bind(this));	
		this.getEle("ul.select-content").addEvent('click:relay(li.select_option_value)',this.getFromUpList.bind(this));
		
	}
	,"onNotFound":function(evt){
	
	}
	,"reSizeList":function(evt){
		evt.preventDefault();		
		if($$('#uid_'+this.uid+'_2').getStyle("display")=="block"){
			this.showList(false).setStyles({"width":(this.getEle().getSize().x-30)+"px"});
		}else if(this.isEnable(2)){
			this.showList(true);
		}		
	}
	,"showList":function(p_on){
		return $$('#uid_'+this.uid+'_2').setStyle("display",(p_on)?"block":"none");	
	}
	,"getFromUpList":function(evt){
			evt.preventDefault();		
			var p_vl = evt.target.get("data-vl");
			this.getInput().set("value",evt.target.get("text"));
			//console.log("selecionado: "+p_vl+" txt: "+evt.target.get("text"));		
			this.getEle().set({"data-prevalue":p_vl,"data-vl":p_vl});		
			this.showList(false);	
	}
	,"setFilter":function(evt){		
		//if(evt.key == "enter"){
			//console.log(evt.key);		
			if(evt.key == "down"){
				//.fireEvent('click');
			}else if(evt.key == "enter"){
				//$$('#uid_'+this.uid+'_2 li.select_option_value')
			}else{
				$$('#uid_'+this.uid+'_2').setStyles({"display":"block"});
				var p_filter = evt.target.get("value").toLowerCase();
				$$('#uid_'+this.uid+'_2 li.select_option_value').each(function(item){
						if(item.get("text").toLowerCase().indexOf(p_filter)>-1){
							item.setStyle("display","block");
						}else{
							item.setStyle("display","none");
						};
				});
			}		
		//}		
	}
	,"setValueField":function(p_column){
		this._valuefield = p_column;
	}
	,"setLabelField":function(p_column){
		this._labelfield = p_column;
	}
	,"getValueField":function(){
		return this._valuefield;
	}
	,"getLabelField":function(){
		return this._labelfield;
	}	
	,"setDataProvider":function(p_dta){
		var dta_t = new js.underas.util.ArrayList(p_dta);
		var tm = dta_t.size();
		var t_html = "";
		for(var x = 0;x<tm;x++){
			var itemS = dta_t.get(x);
			//<li role="presentation"><a role="menuitem" tabindex="-1" href="#">Action</a></li>
			t_html+='<li role="presentation" data-vl="'+itemS[this.getValueField()]+'" data-text="'+itemS[this.getLabelField()]+'" class="select_option_value"><a role="menuitem" tabindex="-1" data-vl="'+itemS[this.getValueField()]+'" href="#">'+itemS[this.getLabelField()]+'</a></li>';
		};
		$$('#uid_'+this.uid+'_2').set("html",t_html);
		t_html = null;
		dta_t.removeAll();
		dta_t = null;
	}
	,"getValue":function(){
		return	this.getEle().get("data-vl");
	}
	,"getText":function(){
		return	this.getValue();
	}
	,"setValue":function(p_vl){	
		this.getEle().set({"data-prevalue":p_vl,"data-vl":p_vl});
		var tmpDesc = this.getDescFromServiceByValue(p_vl);
		this.getInput().set("value",tmpDesc);
	}
	,"getDescFromServiceByValue":function(p_vl){
		var tmpDesc = "";
		$$('#uid_'+this.uid+'_2 li.select_option_value').some(function(item){
			if(item.get("data-vl")==p_vl){		
				tmpDesc	= item.get("data-text");			
				return true;
			};
			return false;
		});
		return tmpDesc;
	}
	,"reloadService":function(){
		//this.setVL(this.getVL()+this.stepvl);
		var _serv = this.getEle().get("data-serv");
		if(_serv){
			this.fromService(_serv);
		}		
	}
	,"fromService":function(p_service){
		this.getEle().set("data-serv",p_service);		
		js.underas.net.RequestManager.addRequest({
			"url":p_service
			,"puid":this.getVarModule()
			,"onLoad":function(rs){
				this.setDataProvider(rs);
				var _prevl = this.getEle().get("data-prevalue");
				if(_prevl){
					this.setValue(_prevl);
				}				
			}.bind(this)
		});
	}
});
